<template>
  <div>
    <div class="block">
      <el-date-picker v-model="value" placeholder="选择日期范围" :picker-options="dateStart" @change="selectTime">
      </el-date-picker>
    </div>
  </div>
</template>

<script>
  import { formatterTime } from '../../assets/js/common.js'

  export default {
    props: ['banListDay', 'banWeek', 'limitField', 'setTime'],//banList禁用日期列表,banWeek是否禁用周几，limitField开始结束日期选取范围,setTime编辑时返回的日期
    data () {
      let _this = this
      return {
        dateStart: {
          disabledDate (time) {
            //banListDay禁用
            for (var i = 0; i < _this.banListDay.length; i++) {
              if (_this.banListDay[i].holiday_date_start === _this.banListDay[i].holiday_date_end) {
                if (new Date(_this.banListDay[i].holiday_date_start).getTime() === time.getTime()) {
                  return true
                }
              } else if (_this.banListDay[i].holiday_date_start !== _this.banListDay[i].holiday_date_end) {
                if (new Date(_this.banListDay[i].holiday_date_start).getTime() < time.getTime()) {
                  if (new Date(_this.banListDay[i].holiday_date_end).getTime() > time.getTime()) {
                    return true
                  }
                }
              }
            }
            //banWeek禁用
            if (_this.banWeek === true) {
              if (time.getDay() === 6 || time.getDay() === 0) {
                return true
              }
            }
            //日期范围禁用
            if (_this.limitField.type === 'after') {
              if (time.getTime() > new Date(_this.limitField.time).getTime())
                return true
            } else if (_this.limitField.type === 'before') {
              if (time.getTime() < new Date(_this.limitField.time).getTime())
                return true
            }
          },
        },
        value: '',
        returnTime: {
          banListDay: [],
          banWeek: [],
          limitField: {},
          returnDate: null,
        },
      }
    },
    methods: {
      selectTime () {
        this.returnTime.banListDay = this.banListDay
        this.returnTime.banWeek = this.banWeek
        this.returnTime.limitField = this.limitField
        this.returnTime.returnDate = this.value
        this.returnTime.returnDate = formatterTime('date', this.returnTime.returnDate)
        this.$emit('returnDate', this.returnTime)
      },
      setDate () {
        if (this.setTime !== '') {
          this.value = this.setTime
        }
      }
    },
    created () {
      this.setDate()
    }
  }
</script>
